﻿using AutoMapper;
using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.PhysicalExaminationItemsCommand;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.PhysicalExaminationItemsHandler
{
    /// <summary>
    /// 体检项目查询接口实现类
    /// </summary>
    public class PhysicalExaminationItemsHandler : IRequestHandler<PhysicalExaminationItemsCommand, APIPaging<PhysicalExaminationItems>>
    {
        private readonly IBaseRepository<PhysicalExaminationItems> baseRepository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository">仓储</param>
        /// <param name="mapper">映射</param>
        public PhysicalExaminationItemsHandler(IBaseRepository<PhysicalExaminationItems> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 查询体检项目接口实现方法
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">参数</param>
        /// <returns></returns>
        public async Task<APIPaging<PhysicalExaminationItems>> Handle(PhysicalExaminationItemsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<PhysicalExaminationItems> result = new APIPaging<PhysicalExaminationItems>();
                result.Code = APIEnum.Mvc;
                result.Msg = "查询成功";
                var list = baseRepository.GetAll().ToList();

                result.TotalCount = list.Count();
                result.PageCount = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                result.Data = list.OrderByDescending(a => a.PhysicalExaminationItemsId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
